﻿module Steps



open System.Runtime.Serialization
open Sources
open Tokens
open Keywords



[<DataContract>]
type StepKind = 
   | Given
   | When
   | Then



[<DataContract>]
type public Step(kind, keyword, instructions) = 
   
   [<DataMember>]
   member val Kind : StepKind = kind with get, set

   [<DataMember>]
   member val Keyword : Token = keyword with get, set

   [<DataMember>]
   member val Instructions : Token[] = instructions with get, set
   


// TODO: NEEDS TESTS
let ReadInstructions(reader : SourceReader) =
   let rec recurse() =      
      let token = ReadToken(reader)

      if (token.IsNone) then
         List.Empty 
      elif (IsKeyword(token.Value)) then
         reader.SetLocation(token.Value.Location)
         List.Empty      
      else
         token.Value :: recurse()

   recurse()
   


// TODO: NEEDS TESTS
let ReadStep(reader : SourceReader, kind : StepKind) =
   let keyword =
      match kind with
      | Given -> ReadKeyword(reader, Keyword.Given)
      | When -> ReadKeyword(reader, Keyword.When)
      | Then -> ReadKeyword(reader, Keyword.Then)

   if (keyword.IsNone) then
      None
   else
      let instructions = ReadInstructions(reader)
      
      Some(Step(kind, keyword.Value, instructions |> List.toArray))

